package com.atguigu.day07;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Flink01_KeyedState_ValueState {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);


        //3.使用map将度过来的数据转为WaterSensor
        SingleOutputStreamOperator<WaterSensor> result = streamSource.map(
                new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
                    }
                });

        //4.将相同ID的数据聚合到一块
        KeyedStream<WaterSensor, Tuple> keyedStream = result.keyBy("id");

        //TODO 5.检测传感器的水位线值，如果连续的两个水位线差值超过10，就输出报警。
        keyedStream.process(new KeyedProcessFunction<Tuple, WaterSensor, String>() {
            //TODO a.定义状态
            private ValueState<Integer> lastVcState;

            //TODO b.初始化状态
            @Override
            public void open(Configuration parameters) throws Exception {
                lastVcState = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("valueState", Integer.class));
            }

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                //TODO c.使用状态
                //1.从状态中取出上一次的水位
//                Integer lastVc = lastVcState.value() == null ? 0 : lastVcState.value();
                Integer lastVc;
                if (lastVcState.value() == null) {
                    lastVc = 0;
                } else {
                    lastVc = lastVcState.value();
                }

                //2.判断水位差
                if (Math.abs(value.getVc() - lastVc) > 10) {
                    out.collect("警报！！！！！！");
                }

                //3.更新状态，将当前水位更新到状态中以供下一条数据使用
                lastVcState.update(value.getVc());

            }
        }).print();

        env.execute();
    }

}
